package lian_xi;

import java.util.concurrent.ThreadFactory;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;

//使用原子类解决数据同步问题
public class ThreadDemo2 {
    public static AtomicInteger money = new AtomicInteger();
    public static void main(String[] args) throws InterruptedException {
    int[] data = new int[]{100,200,300};
    for (int i = 0; i < data.length; i++){
        final int temp = i;
        new Thread(()->{
            try{
                TimeUnit.MILLISECONDS.sleep(100);
            }catch (InterruptedException e){
                money.addAndGet(data[temp]);
            }
        }).start();
    }
        TimeUnit.SECONDS.sleep(1);
        System.out.println("【计算完成】最终存款总额："+money);
    }
}
